<?php

namespace App\Core\Utils\Wechat;

use App\Core\Utils\TwHelper\CommFunction;
use App\Core\Utils\Wechat\WechatConfig;

/**
 * 微信相关操作接口
 */
class WechatUserService
{
    private $accessToken;

    public function __construct()
    {
        $this->accessToken = WechatConfig::getAccessToken();
    }

    /**
     * 根据code获取用户的微信u企业号serid
     * @param $code
     * 参考：http://qydev.weixin.qq.com/wiki/index.php?title=OAuth%E9%AA%8C%E8%AF%81%E6%8E%A5%E5%8F%A3
     */
    public function getUserId($code)
    {
        $params['access_token'] = $this->accessToken;
        $params['code'] = $code;
        //$params['agentid'] = WechatConfig::AGENTID_CLUB;
        $url = sprintf('%s?%s', WechatConfig::WECHAT_OPTION_GET_USER_ID, http_build_query($params));
        $result = CommFunction::getCurl($url);
        if (isset($result ['UserId'])) {
            return $result ['UserId'];
        } else {
            return false;
        }
    }

    /**
     * 根据微信userId获取用户通信录信息
     * @param $userId
     * 参考：http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98#.E8.8E.B7.E5.8F.96.E6.88.90.E5.91.98
     */
    public function getWechatUserByUserId($userId)
    {
        $params['access_token'] = $this->accessToken;
        $params['userid'] = $userId;
        $url = sprintf('%s?%s', WechatConfig::WECHAT_OPTION_USER_GET, http_build_query($params));
        $result = CommFunction::getCurl($url);
        if ($result ['errcode'] == 0) {
            return $result;
        } else {
            return false;
        }
    }

    /**
     * 添加用户信息
     * 参考：http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98#.E5.88.9B.E5.BB.BA.E6.88.90.E5.91.98
     */
    public function addWechatUser($params = array())
    {
        $url = sprintf('%s?%s', WechatConfig::WECHAT_OPTION_USER_ADD, $this->accessToken);
        $result = CommFunction::postCurl($url, json_encode($params));
        if ($result ['errcode'] == 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 修改用户信息
     * 参考：http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98#.E6.9B.B4.E6.96.B0.E6.88.90.E5.91.98
     */
    public function updateWechatUser($params = array())
    {
        $url = sprintf('%s?%s', WechatConfig::WECHAT_OPTION_USER_UPDATE, $this->accessToken);
        $result = CommFunction::postCurl($url, json_encode($params));
        if ($result ['errcode'] == 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 删除用户
     * 参考：http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98#.E6.9B.B4.E6.96.B0.E6.88.90.E5.91.98
     */
    public function delWechatUser($userId)
    {
        $params['access_token'] = $this->accessToken;
        $params['userid'] = $userId;
        $url = sprintf('%s?%s', WechatConfig::WECHAT_OPTION_USER_DEL, http_build_query($params));
        $result = CommFunction::getCurl($url);
        if ($result ['errcode'] == 0) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 批量删除用户信息
     * @param array $params
     * @return bool
     */
    public function delBatchWechatUsers($params = array())
    {
        $data['useridlist'] = $params;
        $url = sprintf('%s?%s', WechatConfig::WECHAT_OPTION_USER_BATCH_DEL, $this->accessToken);
        $result = CommFunction::postCurl($url, json_encode($data));
        if ($result ['errcode'] == 0) {
            return true;
        } else {
            return false;
        }
    }

}